package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TProjectPlan;
import com.blt.govdata.service.ITProjectPlanService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 项目计划前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-27
 */
@RestController
@RequestMapping("/tProjectPlan")
//todo 将所有的XXX修改为真实值
public class TProjectPlanController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITProjectPlanService service;

    /**
     * 根据项目ID查询项目每月计划
     */

    @RequestMapping(value = "/selectPlanByPid", params = {"Pid"})
    public Map<String, Object> selectPlanByPid(Integer Pid, Integer year) {
        if (null == year) year = Calendar.getInstance().get(Calendar.YEAR);
        List<TProjectPlan> result = this.service.selectList(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", Pid)
                .and("examine_state = {0}", Constants.AUDITED_PASS)
                .and("year = {0}", year));
        logger.info("根据项目ID查询项目每月计划");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增项目计划(本月)
     */
    @RequestMapping(value = "/addProjectPlan")
    public Map<String, Object> addProjectPlan(TProjectPlan tProjectPlan) {


        if (null == tProjectPlan || null == tProjectPlan.getpId())
            return ImmutableMap.of("status", "false", "data", false);
        /**
         * 检查新增月份是否存在
         * */
        Integer year = null == tProjectPlan.getYear() ? Calendar.getInstance().get(Calendar.YEAR) : tProjectPlan.getYear();
        TProjectPlan exist = this.service.selectOne(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", tProjectPlan.getpId())
                .and("year = {0}", year)
                .and("month = {0}", tProjectPlan.getMonth()));
        if (null != exist) return ImmutableMap.of("status", "false", "data", "该月份已经存在");
        boolean result = this.service.insert(tProjectPlan);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑项目计划（不能修改月份）
     */
    @RequestMapping(value = "/editProjectPlan")
    public Map<String, Object> editProjectPlan(TProjectPlan tProjectPlan) {
        if (null == tProjectPlan || tProjectPlan.getId() == null)
            return ImmutableMap.of("status", "false", "data", false);
        boolean result = this.service.updateById(tProjectPlan);
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 根据ID查询项目问题
     */
    @RequestMapping(value = "/selectProjectPlanById", params = {"id"})
    public Map<String, Object> selectProjectPlanById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }

}
